<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xmlhttp.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_xmlhttp.js.html">xmlhttp.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Low level handling of XMLHttpRequest.
<a name="line17"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line18"></a> * @author dbk@google.com (David Barrett-Kahn)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.net.DefaultXmlHttpFactory&#39;);
<a name="line22"></a>goog.provide(&#39;goog.net.XmlHttp&#39;);
<a name="line23"></a>goog.provide(&#39;goog.net.XmlHttp.OptionType&#39;);
<a name="line24"></a>goog.provide(&#39;goog.net.XmlHttp.ReadyState&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.net.WrapperXmlHttpFactory&#39;);
<a name="line27"></a>goog.require(&#39;goog.net.XmlHttpFactory&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Static class for creating XMLHttpRequest objects.
<a name="line32"></a> * @return {!(XMLHttpRequest|GearsHttpRequest)} A new XMLHttpRequest object.
<a name="line33"></a> */
<a name="line34"></a>goog.net.XmlHttp = function() {
<a name="line35"></a>  return goog.net.XmlHttp.factory_.createInstance();
<a name="line36"></a>};
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * @define {boolean} Whether to assume XMLHttpRequest exists. Setting this to
<a name="line41"></a> *     true strips the ActiveX probing code.
<a name="line42"></a> */
<a name="line43"></a>goog.net.XmlHttp.ASSUME_NATIVE_XHR = false;
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * Gets the options to use with the XMLHttpRequest objects obtained using
<a name="line48"></a> * the static methods.
<a name="line49"></a> * @return {Object} The options.
<a name="line50"></a> */
<a name="line51"></a>goog.net.XmlHttp.getOptions = function() {
<a name="line52"></a>  return goog.net.XmlHttp.factory_.getOptions();
<a name="line53"></a>};
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Type of options that an XmlHttp object can have.
<a name="line58"></a> * @enum {number}
<a name="line59"></a> */
<a name="line60"></a>goog.net.XmlHttp.OptionType = {
<a name="line61"></a>  /**
<a name="line62"></a>   * Whether a goog.nullFunction should be used to clear the onreadystatechange
<a name="line63"></a>   * handler instead of null.
<a name="line64"></a>   */
<a name="line65"></a>  USE_NULL_FUNCTION: 0,
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * NOTE(user): In IE if send() errors on a *local* request the readystate
<a name="line69"></a>   * is still changed to COMPLETE.  We need to ignore it and allow the
<a name="line70"></a>   * try/catch around send() to pick up the error.
<a name="line71"></a>   */
<a name="line72"></a>  LOCAL_REQUEST_ERROR: 1
<a name="line73"></a>};
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Status constants for XMLHTTP, matches:
<a name="line78"></a> * http://msdn.microsoft.com/library/default.asp?url=/library/
<a name="line79"></a> *   en-us/xmlsdk/html/0e6a34e4-f90c-489d-acff-cb44242fafc6.asp
<a name="line80"></a> * @enum {number}
<a name="line81"></a> */
<a name="line82"></a>goog.net.XmlHttp.ReadyState = {
<a name="line83"></a>  /**
<a name="line84"></a>   * Constant for when xmlhttprequest.readyState is uninitialized
<a name="line85"></a>   */
<a name="line86"></a>  UNINITIALIZED: 0,
<a name="line87"></a>
<a name="line88"></a>  /**
<a name="line89"></a>   * Constant for when xmlhttprequest.readyState is loading.
<a name="line90"></a>   */
<a name="line91"></a>  LOADING: 1,
<a name="line92"></a>
<a name="line93"></a>  /**
<a name="line94"></a>   * Constant for when xmlhttprequest.readyState is loaded.
<a name="line95"></a>   */
<a name="line96"></a>  LOADED: 2,
<a name="line97"></a>
<a name="line98"></a>  /**
<a name="line99"></a>   * Constant for when xmlhttprequest.readyState is in an interactive state.
<a name="line100"></a>   */
<a name="line101"></a>  INTERACTIVE: 3,
<a name="line102"></a>
<a name="line103"></a>  /**
<a name="line104"></a>   * Constant for when xmlhttprequest.readyState is completed
<a name="line105"></a>   */
<a name="line106"></a>  COMPLETE: 4
<a name="line107"></a>};
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * The global factory instance for creating XMLHttpRequest objects.
<a name="line112"></a> * @type {goog.net.XmlHttpFactory}
<a name="line113"></a> * @private
<a name="line114"></a> */
<a name="line115"></a>goog.net.XmlHttp.factory_;
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * Sets the factories for creating XMLHttpRequest objects and their options.
<a name="line120"></a> * @param {Function} factory The factory for XMLHttpRequest objects.
<a name="line121"></a> * @param {Function} optionsFactory The factory for options.
<a name="line122"></a> * @deprecated Use setGlobalFactory instead.
<a name="line123"></a> */
<a name="line124"></a>goog.net.XmlHttp.setFactory = function(factory, optionsFactory) {
<a name="line125"></a>  goog.net.XmlHttp.setGlobalFactory(new goog.net.WrapperXmlHttpFactory(
<a name="line126"></a>      /** @type {function() : !(XMLHttpRequest|GearsHttpRequest)} */ (factory),
<a name="line127"></a>      /** @type {function() : !Object}*/ (optionsFactory)));
<a name="line128"></a>};
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>/**
<a name="line132"></a> * Sets the global factory object.
<a name="line133"></a> * @param {!goog.net.XmlHttpFactory} factory New global factory object.
<a name="line134"></a> */
<a name="line135"></a>goog.net.XmlHttp.setGlobalFactory = function(factory) {
<a name="line136"></a>  goog.net.XmlHttp.factory_ = factory;
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Default factory to use when creating xhr objects.  You probably shouldn&#39;t be
<a name="line143"></a> * instantiating this directly, but rather using it via goog.net.XmlHttp.
<a name="line144"></a> * @extends {goog.net.XmlHttpFactory}
<a name="line145"></a> * @constructor
<a name="line146"></a> */
<a name="line147"></a>goog.net.DefaultXmlHttpFactory = function() {
<a name="line148"></a>  goog.net.XmlHttpFactory.call(this);
<a name="line149"></a>};
<a name="line150"></a>goog.inherits(goog.net.DefaultXmlHttpFactory, goog.net.XmlHttpFactory);
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/** @override */
<a name="line154"></a>goog.net.DefaultXmlHttpFactory.prototype.createInstance = function() {
<a name="line155"></a>  var progId = this.getProgId_();
<a name="line156"></a>  if (progId) {
<a name="line157"></a>    return new ActiveXObject(progId);
<a name="line158"></a>  } else {
<a name="line159"></a>    return new XMLHttpRequest();
<a name="line160"></a>  }
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/** @override */
<a name="line165"></a>goog.net.DefaultXmlHttpFactory.prototype.internalGetOptions = function() {
<a name="line166"></a>  var progId = this.getProgId_();
<a name="line167"></a>  var options = {};
<a name="line168"></a>  if (progId) {
<a name="line169"></a>    options[goog.net.XmlHttp.OptionType.USE_NULL_FUNCTION] = true;
<a name="line170"></a>    options[goog.net.XmlHttp.OptionType.LOCAL_REQUEST_ERROR] = true;
<a name="line171"></a>  }
<a name="line172"></a>  return options;
<a name="line173"></a>};
<a name="line174"></a>
<a name="line175"></a>
<a name="line176"></a>/**
<a name="line177"></a> * The ActiveX PROG ID string to use to create xhr&#39;s in IE. Lazily initialized.
<a name="line178"></a> * @type {string|undefined}
<a name="line179"></a> * @private
<a name="line180"></a> */
<a name="line181"></a>goog.net.DefaultXmlHttpFactory.prototype.ieProgId_;
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Initialize the private state used by other functions.
<a name="line186"></a> * @return {string} The ActiveX PROG ID string to use to create xhr&#39;s in IE.
<a name="line187"></a> * @private
<a name="line188"></a> */
<a name="line189"></a>goog.net.DefaultXmlHttpFactory.prototype.getProgId_ = function() {
<a name="line190"></a>  if (goog.net.XmlHttp.ASSUME_NATIVE_XHR) {
<a name="line191"></a>    return &#39;&#39;;
<a name="line192"></a>  }
<a name="line193"></a>
<a name="line194"></a>  // The following blog post describes what PROG IDs to use to create the
<a name="line195"></a>  // XMLHTTP object in Internet Explorer:
<a name="line196"></a>  // http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
<a name="line197"></a>  // However we do not (yet) fully trust that this will be OK for old versions
<a name="line198"></a>  // of IE on Win9x so we therefore keep the last 2.
<a name="line199"></a>  if (!this.ieProgId_ &amp;&amp; typeof XMLHttpRequest == &#39;undefined&#39; &amp;&amp;
<a name="line200"></a>      typeof ActiveXObject != &#39;undefined&#39;) {
<a name="line201"></a>    // Candidate Active X types.
<a name="line202"></a>    var ACTIVE_X_IDENTS = [&#39;MSXML2.XMLHTTP.6.0&#39;, &#39;MSXML2.XMLHTTP.3.0&#39;,
<a name="line203"></a>                           &#39;MSXML2.XMLHTTP&#39;, &#39;Microsoft.XMLHTTP&#39;];
<a name="line204"></a>    for (var i = 0; i &lt; ACTIVE_X_IDENTS.length; i++) {
<a name="line205"></a>      var candidate = ACTIVE_X_IDENTS[i];
<a name="line206"></a>      /** @preserveTry */
<a name="line207"></a>      try {
<a name="line208"></a>        new ActiveXObject(candidate);
<a name="line209"></a>        // NOTE(user): cannot assign progid and return candidate in one line
<a name="line210"></a>        // because JSCompiler complaings: BUG 658126
<a name="line211"></a>        this.ieProgId_ = candidate;
<a name="line212"></a>        return candidate;
<a name="line213"></a>      } catch (e) {
<a name="line214"></a>        // do nothing; try next choice
<a name="line215"></a>      }
<a name="line216"></a>    }
<a name="line217"></a>
<a name="line218"></a>    // couldn&#39;t find any matches
<a name="line219"></a>    throw Error(&#39;Could not create ActiveXObject. ActiveX might be disabled,&#39; +
<a name="line220"></a>                &#39; or MSXML might not be installed&#39;);
<a name="line221"></a>  }
<a name="line222"></a>
<a name="line223"></a>  return /** @type {string} */ (this.ieProgId_);
<a name="line224"></a>};
<a name="line225"></a>
<a name="line226"></a>
<a name="line227"></a>//Set the global factory to an instance of the default factory.
<a name="line228"></a>goog.net.XmlHttp.setGlobalFactory(new goog.net.DefaultXmlHttpFactory());
</pre>


</body>
</html>
